<template>
<div class="login-container" :style="{backgroundImage:`url(${bg})`}">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

        <div class="title-container">
          
        </div>

        <el-tabs v-model="loginType" @tab-click="changeLoginType">
            <el-tab-pane label="账号登录" name="account">
                <el-form-item prop="username">
                    <span class="svg-container">
                        <svg-icon icon-class="user" />
                    </span>
                    <el-input ref="username" v-model="loginForm.username" placeholder="用户名" name="username" type="text" tabindex="1" auto-complete="on" />
                </el-form-item>

                <el-form-item prop="password">
                    <span class="svg-container">
                        <svg-icon icon-class="password" />
                    </span>
                    <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
                    <span class="show-pwd" @click="showPwd">
                        <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                    </span>
                </el-form-item>

                <el-form-item prop="verify">
                    <div class="verify-group">
                        <el-input class="input" v-model="loginForm.verify" placeholder="验证码" type="text" tabindex="3" auto-complete="on" />
                        <img :src="verifyImg" class="verify" @click="getVerify" />
                    </div>
                </el-form-item>
                <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
            </el-tab-pane>
            <el-tab-pane label="企业微信" name="work_wechat">
                <div id="ww_login"></div>
            </el-tab-pane>
        </el-tabs>

    </el-form>
</div>
</template>

<script>
import * as ww from '@wecom/jssdk';

import {
    getWechatApi
} from '@/api/ucenter/user';
import {
    getVerifyApi
} from "@/api/passport";
import {
    initPickerApi
} from '@/api/system/dictionary/dictionary';
export default {
    name: 'Login',
    data() {
        return {
            wwLogin: null,
            loginType: 'account',
            loginForm: {
                username: '',
                password: '',
                verify: '',
                uuid: ''
            },
            loginRules: {
                username: [{
                    required: true,
                    trigger: 'blur',
                    message: '用户名不能为空'
                }],
                password: [{
                    required: true,
                    trigger: 'blur',
                    message: '密码不能为空'
                }],
                verify: [{
                    required: true,
                    trigger: 'blur',
                    message: '验证码不能为空'
                }],
                uuid: [{
                    required: true,
                    trigger: 'blur',
                    message: '验证码异常'
                }]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            workWechat: {
                corp_id: "",
                agent_id: "",
                redirect_uri: "",
                state: "webLogin"
            },
            bg: '',
            verifyImg: ''
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    created() {
        this.getSystemConfig();
        this.getVerify();
    },
    methods: {
        changeLoginType() {
            if (this.loginType === 'work_wechat') {
                this.showWeiXinQr();
            } else {
                this.wwLogin && this.wwLogin.unmount();
            }
        },
        async getVerify() {
            await getVerifyApi({}).then(res => {
                this.verifyImg = res.data.image;
                this.loginForm.uuid = res.data.uniqid;
            })
        },
        showWeiXinQr() {
            let that = this;
            this.wwLogin = ww.createWWLoginPanel({
                el: '#ww_login',
                params: {
                    login_type: 'CorpApp',
                    appid: this.workWechat.corp_id,
                    agentid: this.workWechat.agent_id,
                    redirect_uri: this.workWechat.redirect_uri,
                    state: 'WWLogin',
                    redirect_type: 'callback',
                },
                onCheckWeComLogin(res) {
                    console.log(res);
                },
                onLoginSuccess({
                    code
                }) {
                    that.handleQrLogin(code);
                },
                onLoginFail(err) {

                },
            })
        },
        getSystemConfig() {
            getWechatApi({}).then(res => {
                if (res.data && res.data.corp_id) this.workWechat = Object.assign(this.workWechat, res.data);
            })
        },
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        handleLogin() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$store.dispatch('user/login', this.loginForm).then(() => {
                        this.loading = false;
                        this.initPicker();
                        this.$router.push({
                            path: this.redirect || '/'
                        })

                    }).catch(() => {
                        this.loading = false;
                        this.getVerify();

                    })
                } else {
                    return false
                }
            })
        },
        handleQrLogin(code) {
            let params = {
                code: code,
                state: 'WWLogin'
            };

            this.$store.dispatch('user/qrlogin', params).then(() => {
                this.initPicker();
                this.$router.push({
                    path: this.redirect || '/'
                })

            }).catch((res) => {
                this.$message.error(res.msg)
            })
        },
        initPicker() {
            initPickerApi().then(res => {
                if (res.code == 200) {
                    let items = res.data;
                    for (let key in items) {
                        let cacheName = key + 'PickerList';
                        localStorage.setItem(cacheName, JSON.stringify(items[key]));
                    }
                }
            })
        }
    }
}
</script>

<style lang="scss">
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .el-tabs__item {
        color: #fff;
    }

    .el-tabs__item.is-active {
        color: #409EFF;
    }

    .el-tabs__item:hover {
        color: #409EFF;
    }

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 100px 35px 0;
        margin: 0 auto;
        overflow: hidden;
    }

    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;
        margin-bottom: 80px;

        .logo {
            width: 50%;
            height: 50%;
            margin: 0 auto;
            display: block;
        }

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .verify-group {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;

        .input {
            flex: 1;
        }

        .verify {
            height: 47px;
        }
    }
}
</style>
